<template>
  <div id="app">
    <!-- navbar -->
    <!-- <nav-bar :title="title" /> -->
    <router-view />
    <!-- tabbar -->
    <tab-bar></tab-bar>
  </div>
</template>

<script>
import Tabbar from './components/Tabbar.vue'
export default {
  name: 'app',
  components: {
    'tab-bar': Tabbar
  },
  data() {
    return {}
  },
  created() {
    // 获取微信授权
    // 发送 code 进行授权验证
    this.$http
      .get(`/base/user_info?code=${this.getQeuryStringByName('code')}`)
      .then(({ data: res }) => {
        const { data } = res
        // 存储到localStorage中
        this.saveToStorage(data)
      })
      .catch(error => {
        console.log('获取授权发生错误', error.message)
      })
  },
  methods: {
    getQeuryStringByName(name) {
      const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
      const r = window.location.search.substr(1).match(reg)
      if (r) {
        return decodeURIComponent(r[2])
      }
      return null
    },
    // 保存授权登陆的用户信息
    saveToStorage(userInfo) {
      if (!window.localStorage.getItem('userInfo')) {
        console.log('保存用户信息，', typeof userInfo, userInfo)
        window.localStorage.setItem('userInfo', JSON.stringify(userInfo))
        window.localStorage.setItem('openId', userInfo.openId)
      }
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-color: #f7f8fa;
  color: #2c3e50;
  /* margin-top:48px; */
}
</style>
